<?php
include_once ("connect.php");
include_once ("log_order.php"); //test!
$action = $_GET['action'];
switch ($action) {
	case 'edit' : 
		$order_id = htmlspecialchars(stripslashes(trim($_POST['order_id'])));
		$user_id = htmlspecialchars(stripslashes(trim($_POST['user_id'])));
		
		$package_name = htmlspecialchars(stripslashes(trim($_POST['package_name'])));
		$package_weight = htmlspecialchars(stripslashes(trim($_POST['package_weight'])));
		$package_length = htmlspecialchars(stripslashes(trim($_POST['package_length'])));
		$package_width = htmlspecialchars(stripslashes(trim($_POST['package_width'])));
		$package_height = htmlspecialchars(stripslashes(trim($_POST['package_height'])));
		
		$item_name1 = htmlspecialchars(stripslashes(trim($_POST['item_name1'])));
		$item_madein1 = htmlspecialchars(stripslashes(trim($_POST['item_madein1'])));
		$item_amount1 = htmlspecialchars(stripslashes(trim($_POST['item_amount1'])));
		$item_pre_weight1 = htmlspecialchars(stripslashes(trim($_POST['item_pre_weight1'])));
		$item_pre_price1 = htmlspecialchars(stripslashes(trim($_POST['item_pre_price1'])));
		$item_weight1 = htmlspecialchars(stripslashes(trim($_POST['item_weight1'])));
		$item_price1 = htmlspecialchars(stripslashes(trim($_POST['item_price1'])));
		
		$item_name2 = htmlspecialchars(stripslashes(trim($_POST['item_name2'])));
		$item_madein2 = htmlspecialchars(stripslashes(trim($_POST['item_madein2'])));
		$item_amount2 = htmlspecialchars(stripslashes(trim($_POST['item_amount2'])));
		$item_pre_weight2 = htmlspecialchars(stripslashes(trim($_POST['item_pre_weight2'])));
		$item_pre_price2 = htmlspecialchars(stripslashes(trim($_POST['item_pre_price2'])));
		$item_weight2 = htmlspecialchars(stripslashes(trim($_POST['item_weight2'])));
		$item_price2 = htmlspecialchars(stripslashes(trim($_POST['item_price2'])));		
		
		$item_name3 = htmlspecialchars(stripslashes(trim($_POST['item_name3'])));
		$item_madein3 = htmlspecialchars(stripslashes(trim($_POST['item_madein3'])));
		$item_amount3 = htmlspecialchars(stripslashes(trim($_POST['item_amount3'])));
		$item_pre_weight3 = htmlspecialchars(stripslashes(trim($_POST['item_pre_weight3'])));
		$item_pre_price3 = htmlspecialchars(stripslashes(trim($_POST['item_pre_price3'])));
		$item_weight3 = htmlspecialchars(stripslashes(trim($_POST['item_weight3'])));
		$item_price3 = htmlspecialchars(stripslashes(trim($_POST['item_price3'])));
		
		$item1_id = 0;		
		$where = " AND item_name='$item_name1' AND item_madein='$item_madein1' AND item_amount=$item_amount1 AND item_pre_weight='$item_pre_weight1' AND item_pre_price='$item_pre_price1' AND item_weight='$item_weight1' AND item_price='$item_price1'";
		$sql = "SELECT id FROM order_item WHERE order_user_id=$user_id".$where." ORDER by id desc";	
		$logger->debug($sql);  //test!
		$rs = mysql_query($sql);
		if ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
			$item1_id = $row['id'];
		} else {
			$sql1 = "INSERT INTO order_item(order_user_id,item_name,item_madein,item_amount,item_pre_weight,item_pre_price,item_weight,item_price,update_time) VALUES($user_id,'$item_name1','$item_madein1',$item_amount1,'$item_pre_weight1','$item_pre_price1','$item_weight1','$item_price1',now())";
			$logger->debug($sql1);  //test!
			mysql_query($sql1);
			
			$logger->debug($sql);  //test!
			$rs = mysql_query($sql);
			if ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
				$item1_id = $row['id'];
			} else {	
				die("操作失败1");
			}
		}
			
		$item2_id = 0;		
		if(!empty($item_name2) && !empty($item_madein2) && !empty($item_amount2) && !empty($item_pre_weight2) && !empty($item_pre_price2) && !empty($item_weight2) && !empty($item_price2) ){		
			$where = " AND item_name='$item_name2' AND item_madein='$item_madein2' AND item_amount=$item_amount2 AND item_pre_weight='$item_pre_weight2' AND item_pre_price='$item_pre_price2' AND item_weight='$item_weight2' AND item_price='$item_price2'";
			$sql = "SELECT id FROM order_item WHERE order_user_id=$user_id".$where." ORDER by id desc";	
			$logger->debug($sql);  //test!
			$rs = mysql_query($sql);
			if ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
				$item2_id = $row['id'];
			} else {
				$sql2 = "INSERT INTO order_item(order_user_id,item_name,item_madein,item_amount,item_pre_weight,item_pre_price,item_weight,item_price,update_time) VALUES($user_id,'$item_name2','$item_madein2',$item_amount2,'$item_pre_weight2','$item_pre_price2','$item_weight2','$item_price2',now())";
				$logger->debug($sql2);  //test!
				mysql_query($sql2);
				
				$logger->debug($sql);  //test!
				$rs = mysql_query($sql);
				if ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
					$item2_id = $row['id'];
				} else {	
					die("操作失败2");
				}
			}
		}
		
		$item3_id = 0;
		if(!empty($item_name3) && !empty($item_madein3) && !empty($item_amount3) && !empty($item_pre_weight3) && !empty($item_pre_price3) && !empty($item_weight3) && !empty($item_price3) ){
			$where = " AND item_name='$item_name3' AND item_madein='$item_madein3' AND item_amount=$item_amount3 AND item_pre_weight='$item_pre_weight3' AND item_pre_price='$item_pre_price3' AND item_weight='$item_weight3' AND item_price='$item_price3'";
			$sql = "SELECT id FROM order_item WHERE order_user_id=$user_id".$where;	
			$logger->debug($sql);  //test!
			$rs = mysql_query($sql);
			if ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
				$item3_id = $row['id'];
			} else {
				$sql3 = "INSERT INTO order_item(order_user_id,item_name,item_madein,item_amount,item_pre_weight,item_pre_price,item_weight,item_price,update_time) VALUES($user_id,'$item_name3','$item_madein3',$item_amount3,'$item_pre_weight3','$item_pre_price3','$item_weight3','$item_price3',now())";
				$logger->debug($sql3);  //test!
				mysql_query($sql3);
				
				$logger->debug($sql);  //test!
				$rs = mysql_query($sql);
				if ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
					$item3_id = $row['id'];
				} else {	
					die("操作失败3");
				}
			}
		}
		
		$logger->debug("item1_id=".$item1_id." item2_id=".$item2_id." item3_id=".$item3_id);  //test!
		
		//---计算金额
		$fee_sum = $package_weight*37;
		$fee_currency = 1 ; 
		
		$order_status=20; //待付款
		
		
		$sql = "UPDATE order_list SET package_name='$package_name',package_weight=$package_weight,package_length=$package_length,package_width=$package_width,package_height=$package_height,item1_id=$item1_id,item2_id=$item2_id,item3_id=$item3_id,fee_sum=$fee_sum,fee_currency=$fee_currency,time_create=now(),order_status=$order_status,update_time=now() WHERE id=$order_id ";	
		$logger->debug($sql);  //test!
		$rs = mysql_query($sql);			
		
		$logger->debug("order_id=".$order_id);  //test!
		
		echo $order_id;
		
		break;	
	
//----------------------------------------------------------------------------------
	case 'list' : //列表		
		$page =  $_GET['page'];
		$limit = $_GET['rows'];
		$sidx =  $_GET['sidx'];
		$sord =  $_GET['sord'];
		if (!$sidx){
			$sidx = 1;
		}

        $where = '';
		
		$user_id  = $_GET['uid'];		
        if(!empty($user_id)){
            $where .= " and order_user_id=".$user_id ;
		}

		$sql = "SELECT COUNT(*) AS count FROM order_item WHERE 1=1".$where;
		//$logger->debug($sql);  //test!						
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$count = $row['count'];		
		if ($count > 0) {
			$total_pages = ceil($count / $limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages)
			$page = $total_pages;
		$start = $limit * $page - $limit;
		if ($start<0) $start = 0;
		$sql = "SELECT * FROM order_item WHERE 1=1".$where." ORDER BY $sidx $sord LIMIT $start , $limit";
		//$logger->debug($sql);  //test!
		$result = mysql_query($sql) or die("Couldn t execute query." . mysql_error());

		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records = $count;
		$i = 0;
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			$responce->rows[$i]['id'] = $row[id];
			$opt = "<a href='order_address.php?id=".$row['id']."'>地址</a>";
			$opt .= "&nbsp;&nbsp;<a href='order_item.php?id=".$row['id']."'>物品</a>";
			$opt .= "&nbsp;&nbsp;<a href='order_detail.php?id=".$row['id']."'>详细</a>";
			
			$responce->rows[$i]['cell'] = array (
				$row['id'],
				$row['item_name'],
				$row['item_amount'],	
				$row['item_pre_weight'],
				$row['item_madein'],
				$row['item_pre_price'],	
				$row['item_price'],
				$row['item_weight'],
				$opt
			);
			$i++;
		}
		echo json_encode($responce);		
		break;		



		
		
	case 'del' : //删除
		$ids = $_POST['ids'];
		delAllSelect($ids, $link);
		break;
	case '' :
		echo 'Bad request.';
		break;
}

//批量删除操作
function delAllSelect($ids, $link) {
	if (empty ($ids))
		die("0");
	mysql_query("update products set deleted=1 where id in($ids)");
	if (mysql_affected_rows($link)) {
		echo $ids;
	} else {
		die("0");
	}
}


//------------------
//处理接收jqGrid提交查询的中文字符串
function unescape($str) 
{ 
    $ret = ''; 
    $len = strlen($str); 
    for ($i = 0; $i < $len; $i ++) 
    { 
        if ($str[$i] == '%' && $str[$i + 1] == 'u') 
        { 
            $val = hexdec(substr($str, $i + 2, 4)); 
            if ($val < 0x7f) 
                $ret .= chr($val); 
            else  
                if ($val < 0x800) 
                    $ret .= chr(0xc0 | ($val >> 6)) . 
                     chr(0x80 | ($val & 0x3f)); 
                else 
                    $ret .= chr(0xe0 | ($val >> 12)) . 
                     chr(0x80 | (($val >> 6) & 0x3f)) . 
                     chr(0x80 | ($val & 0x3f)); 
            $i += 5; 
        } else  
            if ($str[$i] == '%') 
            { 
                $ret .= urldecode(substr($str, $i, 3)); 
                $i += 2; 
            } else 
                $ret .= $str[$i]; 
    } 
    return $ret; 
}



?>